import { login, logout, getInfo } from '@/api/login';
import { getToken, setToken, removeToken, getSite, setSite, removeSite  } from '@/utils/auth';
import fetch from '@/utils/fetch';
// import request from 'request';

const user = {
  state: {
    token: getToken(),
    name: '',
    site: '',
    city: '',
    site_name: '',
    site_subDomain: '',
    roles: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_SITE: (state, site) => {
      state.site = site;
    },
    SET_CITY: (state, city) => {
      state.city = city;
    },
    SET_SITENAME: (state, site_name) => {
      state.site_name = site_name;
    },
    SET_SUBDOMAIN: (state, subDomain) => {
      state.site_subDomain = subDomain;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    }
  },

  actions: {
    // 登录
    Login({ commit, state }, userInfo) {
      const email = userInfo.email.trim();
      return new Promise((resolve, reject) => {
        // const response = {
        //   data: {
        //     token: 'dsfst4et54676ytrfd'
        //   }
        // }
        // console.log('userInfo',userInfo)
        // const data = response.data;
        // console.log(data);
        // setToken(data.token);
        // setSite(userInfo.site);
        // // setName(userInfo.name);
        // commit('SET_TOKEN', data.token);
        // commit('SET_SITE', userInfo.site);
        // resolve();
        login(userInfo.site, email, userInfo.password).then(response => {
            console.log('userInfo', response)
          const data = response.data.result;
          setToken(data.token);
          setSite(userInfo.site);
          commit('SET_TOKEN', data.token);
          resolve();
        }).catch(error => {
          reject(error);
        });
      });
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      console.log('------ -------- -------', state);
      console.log('-------- commit -----',commit)
      return new Promise((resolve, reject) => {
          var site_id = getSite();
          fetch({
              url: '/site/'+ site_id,
              method: 'get'
          }).then(res => {
              if (res.data) {
                  console.log(res.data)
                  const response = {
                    data: {
                      role: res.data.name == '南京站'?'root':'admin',
                      site: res.data._id,
                      city: res.data.city['$id'],
                      name: res.data._id,
                      site_name: res.data.name,
                      subDomain: res.data.subDomain
                    }
                  }
                  const data = response.data;
                  commit('SET_ROLES', data.role);
                  commit('SET_NAME', data.name);
                  commit('SET_SITE', data.site);
                  commit('SET_CITY', data.city);
                  commit('SET_SITENAME', data.site_name);
                  commit('SET_SUBDOMAIN', data.subDomain);
                  resolve(response);
              }
          })



          console.log('-------- Start resolve ---------',resolve);
          console.log('-------- Start reject ---------',reject);
          console.log('----- host --------', window.location.host);


        // getInfo(state.token).then(response => {
        //   const data = response.data;
        //   commit('SET_ROLES', data.role);
        //   commit('SET_NAME', data.name);
        //   commit('SET_AVATAR', data.avatar);
        //   resolve(response);
        // }).catch(error => {
        //   reject(error);
        // });
      });
    },

    // 登出
    LogOut({ commit, state }) {
        console.log('-----------');
        console.log('---- LogGout ------',commit)
      console.log('---- LogGout ------', state)
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '');
          commit('SET_ROLES', []);
          removeToken();
          removeSite();
          resolve();
        }).catch(error => {
          reject(error);
        });
      });
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '');
        removeToken();
        removeSite();
        resolve();
      });
    }
  }
};

export default user;
